
import mx.rpc.events.ResultEvent;
import mx.utils.StringUtil;

protected function register_result(event:ResultEvent):void{
	bi_1.visible = false;
	isLogin = true;
	mId = tput_signUp_id.text;
	mPermission = "준회원";
	onSaveLogin(); // db에 로그인기록.
	onClickBack(); // 이전 화면으로 이동
	
}

protected function onClickMember():void{
	this.currentState = "member";
	checkLogin();
	refreshMemberInfo();
}

protected function getPermission():void{
	bi_1.visible = true;
	var params:Object = new Object();
	params['id'] = mId;
	httpserv_getPermission.send(params);
}

protected function getPermission_result(event:ResultEvent):void{
	bi_1.visible = false;
	mPermission = event.result.toString();
	refreshMemberView();
	
	onSaveLogin();
}

protected function onClickLogout():void{
	isLogin = false;
	mId = "";
	mPermission = "";
	tput_member_permission.text = "비회원";
	delLogin();
	refreshMemberView();
}

protected function refreshMemberView():void{
	tput_member_permission.text = unescape(mPermission);
	tput_member_id.text = mId;
	
	trace(isLogin.toString());
	
	if(isLogin == true){
		lab_noti_member.text = "로그인되었습니다.";
		btn_register.visible = false;
		btn_register.includeInLayout = false;
		btn_member_login.visible = false;
		btn_member_login.includeInLayout = false;
		btn_logout.visible = true;
		btn_logout.includeInLayout = true;
	}else if(isLogin == false){
		lab_noti_member.text = "로그인하십시오.";
		btn_register.visible = true;
		btn_register.includeInLayout = true;
		btn_member_login.visible = true;
		btn_member_login.includeInLayout = true;
		btn_logout.visible = false;
		btn_logout.includeInLayout = false;
	}
}

/** 로그인 여부 */
public var isLogin:Boolean = false;

public var mId:String = "";
public var mPermission:String = "";


/** 가입 버튼 누름 */
protected function onClick_SignUp():void{
	bi_1.visible = true;
	// id 중복체크 httpserv 호출.
	var params:Object = {};
	params['id'] = StringUtil.trim(tput_signUp_id.text);
	httpserv_validate_id.send(params);
}

protected function check_login_result(event:ResultEvent):void{
	bi_1.visible = false;
	trace(event.result.toString());
	if(event.result.toString() == "true"){
		//로그인 완료
		isLogin = true;
		mId = tput_signIn_id.text;
		this.currentState = "main";
		refreshMemberInfo();
		Alert.show(this,"로그인되었습니다.");
	}else{
		//로그인 실패
		Alert.show(this,"로그인에 실패하였습니다. 아이디나 암호를 재확인해주십시오.");
		lab_noti_login.text = "로그인에 실패하였습니다.";
	}
}

/** 로그인 체크 */
protected function checkLogin():void{
	// db 확인 후 isLogin, mId, mPermission 값 할당.
	getLogins();
}

protected function validate_id_result(event:ResultEvent):void{
	bi_1.visible = false;
	if(event.result == "able"){
		validateSignUpForm();
	}else{
		lab_noti_register.text = "이미 존재하는 아이디입니다.";
	}
}

protected function validateSignUpForm():void{
	if(tput_signUp_pwd.text.length == 0 || tput_signUp_reType.text.length == 0){
		lab_noti_register.text = "사용하실 비밀번호를 입력하세요";
	}else if(tput_signUp_pwd.text != tput_signUp_reType.text){
		lab_noti_register.text = "암호가 다릅니다.";
	}else{
		//가입 정보 발신
		bi_1.visible = true;
		var params:Object = {};
		params['id'] = StringUtil.trim(tput_signUp_id.text);
		params['email'] = tput_signUp_email.text;
		params['pwd'] = StringUtil.trim(tput_signUp_pwd.text);
		httpserv_register.send(params);
	}
}

protected function refreshMemberInfo():void{
	getLogins();
	if(mId){
		tput_member_id.text = mId;
		tput_member_permission.text = "등급 확인중...";
		getPermission();
	}
	refreshMemberView();
}